package com.aaa.lockscreen.appwidget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;

import com.aaa.lockscreen.R;

/**
 * create by water.yuan
 * on 2020-11-25
 */
public class AppWidgetReceiver extends AppWidgetProvider {
    private static final String TAG = AppWidgetReceiver.class.getSimpleName();

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        Log.d(TAG, "onUpdate: ");

        // appWidgetIds是系统返回的，即便删除应用后重装，再添加小部件也会累加，说明系统里记录了该值
        final int N = appWidgetIds.length;
        for (int i = 0; i < N; i++) {
            int appWidgetId = appWidgetIds[i];
            Log.d(TAG, "onUpdate: appWidgetId=" + appWidgetId);
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.lock_screen_appwidget);

            // 小部件中PendingIntent通过Broadcast拉起进程
            Intent intent = new Intent(context, ClickReceiver.class);// 8.0+ 显式广播才能拉起静态注册的广播
            intent.setAction("com.aaa.action.lock_screen");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 110, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            // 小部件中PendingIntent通过Service拉不起进程
            // intent = new Intent(context, ClickService.class);
            // pendingIntent = PendingIntent.getService(context, 110, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            // 小部件中PendingIntent通过Activity拉起进程
            // intent = new Intent(context, ClickActivity.class);
            // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // pendingIntent = PendingIntent.getActivity(context, 110, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            views.setOnClickPendingIntent(R.id.pic, pendingIntent);
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

    @Override
    public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
        Log.d(TAG, "onAppWidgetOptionsChanged: ");
    }

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        Log.d(TAG, "onEnabled: ");
    }

    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
        Log.d(TAG, "onDisabled: ");
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
        Log.d(TAG, "onDeleted: ");
    }

    @Override
    public void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
        super.onRestored(context, oldWidgetIds, newWidgetIds);
        Log.d(TAG, "onRestored: ");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        Log.d(TAG, "onReceive: " + intent);
    }
}
